//
//  FeaturedCollectionsTableViewCell.m
//  Beauty
//
//  Created by Snow on 16/8/31.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "FeaturedCollectionsTableViewCell.h"
#import "FeaturedCollectionsCollectionViewCell.h"
#import "GXNetWorkingParser.h"
#import "GXMacro.h"
#import "ModelOfFeaturedCollections.h"
#import "ModelOfGoodList.h"
@interface FeaturedCollectionsTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, FeaturedDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrayOfFeatyreCollections;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *arrayOfGoods;
@end
@implementation FeaturedCollectionsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self handleData];
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.layout.minimumLineSpacing = 5;
        self.layout.minimumInteritemSpacing = 0;
        self.layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.layout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.bounces = NO;
        [self.contentView addSubview:self.collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top);
            make.left.mas_equalTo(self.contentView.mas_left);
            make.right.mas_equalTo(self.contentView.mas_right);
            make.bottom.mas_equalTo(self.contentView.mas_bottom);
        }];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[FeaturedCollectionsCollectionViewCell class] forCellWithReuseIdentifier:@"FeaturedCollectionsCollectionViewCell"];
    }
    return self;
}
- (void)handleData
{
    _arrayOfFeatyreCollections = [NSMutableArray array];
    _arrayOfGoods = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile/index.php?act=index&op=index_1_5" par:nil success:^(id responseObject) {
        NSDictionary *dictionary = [responseObject objectForKey:@"datas"];
        NSArray *array = dictionary[@"special_list"];
        for (NSDictionary *dic in array) {
            ModelOfFeaturedCollections *model = [[ModelOfFeaturedCollections alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_arrayOfFeatyreCollections addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}
- (void)layoutSubviews
{
    [super layoutSubviews];

    self.layout.itemSize = CGSizeMake((self.contentView.bounds.size.width - 40), self.contentView.bounds.size.height);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfFeatyreCollections.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FeaturedCollectionsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FeaturedCollectionsCollectionViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    ModelOfFeaturedCollections *model = [_arrayOfFeatyreCollections objectAtIndex:indexPath.row];
    [cell.imageOfGoods sd_setImageWithURL:[NSURL URLWithString:model.special_banner]];
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dic in model.goods_list) {
        ModelOfGoodList *model = [[ModelOfGoodList alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [array addObject:model];
    }
    cell.arrayOfImages = array;
    return cell;
}
//delegate
- (void)featuredWithGoodId:(NSString *)good_id
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(featuredToMainWithGoodId:)]) {
        [self.delegate featuredToMainWithGoodId:good_id];
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfFeaturedCollections *model = [_arrayOfFeatyreCollections objectAtIndex:indexPath.row];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(featuredToMainClickOnImageWithID:)]) {
        [self.delegate featuredToMainClickOnImageWithID:model.special_id];
    }
}
@end
